/**
 * 
 */
package com.spatialkey.datapoller.communication;

import java.util.Date;

import org.apache.log4j.Logger;

import com.enterprisedt.net.ftp.FTPFile;
import com.enterprisedt.net.ftp.FileTransferClient;

/**
 * Copyright (c) 2008 Universal Mind, Inc.
 * @author Anthony McClure
 * Date: May 8, 2009
 * Time: 1:59:44 PM
 *
 */
public class PollFTP {
	private static Logger logger = Logger.getLogger("com.spatialkey.datapoller.communication.PollFTP");
	
	private String ftpHost;
	private int ftpPort;
    private String ftpUsername;
    private String ftpPassword;
    private boolean ftpDeleteFromRemote = false;
    
    public PollFTP() {
    	super();
    }
    
    /**
	 * @param ftpHost
	 * @param ftpUsername
	 * @param ftpPassword
	 */
	public PollFTP(String ftpHost, int ftpPort, String ftpUsername, String ftpPassword, boolean ftpDeleteFromRemote) {
		super();
		this.ftpHost = ftpHost;
		this.ftpPort = ftpPort;
		this.ftpUsername = ftpUsername;
		this.ftpPassword = ftpPassword;
		this.ftpDeleteFromRemote = ftpDeleteFromRemote;
	}
	
	public boolean checkForFile(String outputDirectory, String processingId)
	{
		try {
			FileTransferClient ftp = new FileTransferClient();
			ftp.setRemoteHost(ftpHost);
			ftp.setPassword(ftpPassword);
			ftp.setUserName(ftpUsername);
			ftp.setRemotePort(ftpPort);
			
			logger.info("[checkForFile] Connecting to: " + ftpHost);
			
			ftp.connect();
			
			FTPFile[] contents = ftp.directoryList();
			
			FTPFile csv = null;
			FTPFile xml = null;
			for (FTPFile file : contents)
			{
				//ignore directories
				if (file.isFile())
				{
					//check for .csv or .xml
					if (csv == null && file.getName().toLowerCase().endsWith(".csv"))
						csv = file;
					else if (xml == null && file.getName().toLowerCase().endsWith(".xml"))
						xml = file;
					
					if (csv != null && xml != null)
						break;
				}
			}
			
			//if no csv found, nothing to process
			if (csv == null)
				return false;
			
			logger.info("[checkForFile] Found a .csv, downloading...");
			
			//download the .csv and .xml
			ftp.downloadFile(outputDirectory + processingId + ".csv", csv.getName());
			
			if (this.ftpDeleteFromRemote)
				ftp.deleteFile(csv.getName());
			else
				ftp.rename(csv.getName(), new Date().getTime() + "_" + csv.getName() + ".done");
			
			ftp.downloadFile(outputDirectory + processingId + ".xml", xml.getName());
			
			ftp.disconnect();
		} catch (Exception e) {
			logger.error("[checkForFile] An error happened during FTP operations: " + e.getMessage());
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
    
	/**
	 * @return the ftpHost
	 */
	public String getFtpHost() {
		return ftpHost;
	}
	/**
	 * @param ftpHost the ftpHost to set
	 */
	public void setFtpHost(String ftpHost) {
		this.ftpHost = ftpHost;
	}
	/**
	 * @return the ftpUsername
	 */
	public String getFtpUsername() {
		return ftpUsername;
	}
	/**
	 * @param ftpUsername the ftpUsername to set
	 */
	public void setFtpUsername(String ftpUsername) {
		this.ftpUsername = ftpUsername;
	}
	/**
	 * @return the ftpPassword
	 */
	public String getFtpPassword() {
		return ftpPassword;
	}
	/**
	 * @param ftpPassword the ftpPassword to set
	 */
	public void setFtpPassword(String ftpPassword) {
		this.ftpPassword = ftpPassword;
	}

	/**
	 * @return the ftpPort
	 */
	public int getFtpPort() {
		return ftpPort;
	}

	/**
	 * @param ftpPort the ftpPort to set
	 */
	public void setFtpPort(int ftpPort) {
		this.ftpPort = ftpPort;
	}

	/**
	 * @return the ftpDeleteFromRemote
	 */
	public boolean isFtpDeleteFromRemote() {
		return ftpDeleteFromRemote;
	}

	/**
	 * @param ftpDeleteFromRemote the ftpDeleteFromRemote to set
	 */
	public void setFtpDeleteFromRemote(boolean ftpDeleteFromRemote) {
		this.ftpDeleteFromRemote = ftpDeleteFromRemote;
	}
}
